package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.mapper.Wms_Purchase_Detail_Mapper;
import com.shop.mapper.Wms_Purchase_Mapper;
import com.shop.pojo.wms.Wms_Purchase;
import com.shop.pojo.wms.Wms_Purchase_Detail;
import com.shop.service.Wms_Purchase_Detail_Service;
import com.shop.utils.CaiGouVo;
import com.shop.utils.LayuiUtils;
import com.shop.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 采购需求-李贝贝
 */
@Service
public class Wms_Purchase_Detail_ServiceImpl extends ServiceImpl<Wms_Purchase_Detail_Mapper,Wms_Purchase_Detail> implements Wms_Purchase_Detail_Service {

    @Autowired
    private Wms_Purchase_Detail_Mapper wms_purchase_detail_mapper;
    //采购单的持久层注入
    @Autowired
    private Wms_Purchase_Mapper wms_purchase_mapper;

    /**
     * 显示所有采购需求
     * @param pages
     * @param wms_purchase_detail
     * @return
     */
    @Override
    public LayuiUtils<Wms_Purchase_Detail> find_all_pruchase_detail(Page<Wms_Purchase_Detail> pages, Wms_Purchase_Detail wms_purchase_detail) {
      LayuiUtils<Wms_Purchase_Detail> layuis = new LayuiUtils<>();
      QueryWrapper<Wms_Purchase_Detail> query = new QueryWrapper<>();
      if (wms_purchase_detail.getWare_id() != null){
          query.eq("ware_id",wms_purchase_detail.getWare_id());
      }
      if (wms_purchase_detail.getStatus() != null){
          query.eq("status",wms_purchase_detail.getStatus());
      }
        //少一个状态的查询
      Page<Wms_Purchase_Detail> wms_purchase_detailPage = wms_purchase_detail_mapper.selectPage(pages,query);
      layuis.setCount(wms_purchase_detailPage.getTotal());
      layuis.setData(wms_purchase_detailPage.getRecords());
        return layuis;
    }

    /**
     * 添加
     * @param wms_purchase_detail
     * @return
     */
    @Override
    public R insert_purchase_detial(Wms_Purchase_Detail wms_purchase_detail) {
        int insert = wms_purchase_detail_mapper.insert(wms_purchase_detail);
        if (insert > 0){
            return R.success();
        }
        return R.error(500,"添加失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public R delete_purchase_detial(List<Long> id) {
        int ids = wms_purchase_detail_mapper.deleteBatchIds(id);
        if (ids > 0){
            return R.success();
        }else {
            return R.error(500,"删除失败");
        }
    }

    /**
     * 修改
     * @param wms_purchase_detail
     * @return
     */
    @Override
    public R update_purchase_detial(Wms_Purchase_Detail wms_purchase_detail) {
        int o = wms_purchase_detail_mapper.updateById(wms_purchase_detail);
        if (o > 0){
            return R.success();
        }else {
            return R.error(500,"修改失败");
        }
    }

    /**
     * 查询采购单的状态   查询条件为新建0 已分配1
     * @return
     */
    @Override
    public List<Wms_Purchase> find_admin_Status_Ling_All() {
        QueryWrapper<Wms_Purchase>  query = new QueryWrapper<>();
        query.eq("status",0).or().eq("status",1);

        return wms_purchase_mapper.selectList(query);
    }


}
